package com.show.business.device.service.impl;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.show.business.common.core.domain.AjaxResult;
import com.show.business.common.utils.StringUtils;
import com.show.business.device.bean.DeviceGroup;
import com.show.business.device.bean.DeviceManage;
import com.show.business.device.bean.vo.DeviceGroupSet;
import com.show.business.device.bean.vo.DeviceLineVo;
import com.show.business.device.mapper.DeviceGroupMapper;
import com.show.business.device.mapper.DeviceManageMapper;
import com.show.business.device.service.IDeviceManageService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.show.business.common.utils.SecurityUtils.getUsername;

/**
 * <p>
 * 设备管理 服务实现类
 * </p>
 *
 * @author dvlp
 * @since 2024-09-30
 */
@Service
public class DeviceManageServiceImpl extends ServiceImpl<DeviceManageMapper, DeviceManage> implements IDeviceManageService {
    @Resource
    private DeviceGroupMapper deviceGroupMapper;

    @Override
    public List<DeviceManage> getList(DeviceManage deviceManage) {
        return baseMapper.getList(deviceManage);
    }

    /**
     * 根据imei查询设备信息
     * @param imei
     * @return
     */
    @Override
    public DeviceManage getDeviceByImei(String imei){
        List<DeviceManage> list=baseMapper.getListByImei(imei);
        if(list!=null && list.size()>0){
            return list.get(0);
        }else{
            return null;
        }
    }

    @Override
    public AjaxResult saveInfo(DeviceManage deviceManage) {
        if(StrUtil.isBlank(deviceManage.getImei())){
            return AjaxResult.error("设备编码不能为空");
        }
        if(StrUtil.isBlank(deviceManage.getImei())){
            return AjaxResult.error("设备IMEI不能为空");
        }
        if(StrUtil.isBlank(deviceManage.getImei())){
            return AjaxResult.error("设备ICCID不能为空");
        }
        if(baseMapper.repeatCodeNum(deviceManage) > 0){
            return AjaxResult.error("设备编码不能重复");
        }
        if(baseMapper.repeatImeiNum(deviceManage) > 0){
            return AjaxResult.error("设备IMEI不能重复");
        }
        if(baseMapper.repeatIccidNum(deviceManage) > 0){
            return AjaxResult.error("设备ICCID不能重复");
        }
        //主线
        if(StringUtils.isNotEmpty(deviceManage.getMonitorMainline()) && deviceManage.getMonitorMainline().equals("0")){
            deviceManage.setMainStrandState("0");
        }
        //设备类型(0-单路；1-多路)
        if (deviceManage.getDeviceType().equals("1")){
            //1号子线
            if(StringUtils.isNotEmpty(deviceManage.getMonitorOneStrand()) && deviceManage.getMonitorOneStrand().equals("0")){
                deviceManage.setOneStrandState("0");
            }
            //2号子线
            if(StringUtils.isNotEmpty(deviceManage.getMonitorTwoStrand()) && deviceManage.getMonitorTwoStrand().equals("0")){
                deviceManage.setTwoStrandState("0");
            }
            //3号子线
            if(StringUtils.isNotEmpty(deviceManage.getMonitorThreeStrand()) && deviceManage.getMonitorThreeStrand().equals("0")){
                deviceManage.setThreeStrandState("0");
            }
            //4号子线
            if(StringUtils.isNotEmpty(deviceManage.getMonitorFourStrand()) && deviceManage.getMonitorFourStrand().equals("0")){
                deviceManage.setFourStrandState("0");
            }
        }
        deviceManage.setDelFlag("0");
        deviceManage.setCreateTime(DateTime.now());
        deviceManage.setCreateBy(getUsername());
        baseMapper.insert(deviceManage);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult editInfo(DeviceManage deviceManage) {
        if(StrUtil.hasBlank(deviceManage.getDeviceCode(),deviceManage.getImei(),deviceManage.getIccid(),deviceManage.getId())){
            return AjaxResult.error("必填项不能为空");
        }
        if(baseMapper.repeatCodeNum(deviceManage) > 0){
            return AjaxResult.error("设备编码不能重复");
        }
        if(baseMapper.repeatImeiNum(deviceManage) > 0){
            return AjaxResult.error("设备IMEI不能重复");
        }
        if(baseMapper.repeatIccidNum(deviceManage) > 0){
            return AjaxResult.error("设备ICCID不能重复");
        }
        //主线
        if(StringUtils.isNotEmpty(deviceManage.getMonitorMainline()) && deviceManage.getMonitorMainline().equals("0") && StringUtils.isNotEmpty(deviceManage.getMainStrandState())){
            deviceManage.setMainStrandState("0");
        }
        //设备类型(0-单路；1-多路)
        if (deviceManage.getDeviceType().equals("1")){
            //1号子线
            if(StringUtils.isNotEmpty(deviceManage.getMonitorOneStrand()) && deviceManage.getMonitorOneStrand().equals("0") && StringUtils.isNotEmpty(deviceManage.getMainStrandState())){
                deviceManage.setOneStrandState("0");
            }
            //2号子线
            if(StringUtils.isNotEmpty(deviceManage.getMonitorTwoStrand()) && deviceManage.getMonitorTwoStrand().equals("0") && StringUtils.isNotEmpty(deviceManage.getTwoStrandState())){
                deviceManage.setTwoStrandState("0");
            }
            //3号子线
            if(StringUtils.isNotEmpty(deviceManage.getMonitorThreeStrand()) && deviceManage.getMonitorThreeStrand().equals("0") && StringUtils.isNotEmpty(deviceManage.getThreeStrandState())){
                deviceManage.setThreeStrandState("0");
            }
            //4号子线
            if(StringUtils.isNotEmpty(deviceManage.getMonitorFourStrand()) && deviceManage.getMonitorFourStrand().equals("0") && StringUtils.isNotEmpty(deviceManage.getFourStrandState())){
                deviceManage.setFourStrandState("0");
            }
        }
        deviceManage.setUpdateTime(DateTime.now());
        deviceManage.setUpdateBy(getUsername());
        baseMapper.updateById(deviceManage);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getInfo(String id) {
        return AjaxResult.success(baseMapper.selectById(id));
    }

    @Override
    public AjaxResult delInfo(String id) {
        baseMapper.updateDel(id);
        return AjaxResult.success();
    }
    @Override
    public AjaxResult delInfos(String[] ids) {
        for(String id:ids){
            baseMapper.updateDel(id);
        }
        return AjaxResult.success();
    }

    /**
     * 启用设备
     * @param deviceId
     * @return
     */
    @Override
    public AjaxResult enable(String deviceId){
        DeviceManage deviceManage=baseMapper.selectById(deviceId);
        deviceManage.setDeviceStatus("0");
        deviceManage.setUpdateTime(DateTime.now());
        deviceManage.setUpdateBy(getUsername());
        baseMapper.updateById(deviceManage);
        return AjaxResult.success();
    }

    /**
     * 禁用设备
     * @param deviceId
     * @return
     */
    @Override
    public AjaxResult disable(String deviceId){
        DeviceManage deviceManage=baseMapper.selectById(deviceId);
        deviceManage.setDeviceStatus("2");
        deviceManage.setUpdateTime(DateTime.now());
        deviceManage.setUpdateBy(getUsername());
        baseMapper.updateById(deviceManage);
        return AjaxResult.success();
    }

    /**
     * 获取分组设备列表查询
     * @param deviceManage
     * @return
     */
    @Override
    public List<DeviceManage> getListByGroup(DeviceManage deviceManage){
        return baseMapper.getListByGroup(deviceManage);
    }

    /**
     * 获取未分组设备列表查询
     * @return
     */
    @Override
    public List<DeviceManage> getListByNoGroup(DeviceManage deviceManage){
        return baseMapper.getListByNoGroup(deviceManage);
    }

    /**
     * 取消设备分组
     */
    @Override
    public AjaxResult groupDeviceCancel(DeviceGroupSet groupSet){
        DeviceManage deviceManage=baseMapper.selectById(groupSet.getDeviceId());
        if(ObjectUtil.isEmpty(deviceManage)){
            return AjaxResult.error("未查询到该设备");
        }

        if(!deviceManage.getGroupId().equals(groupSet.getGroupId())){
            return AjaxResult.error("该设备分配的分组与当前分组不匹配");
        }
        deviceManage.setGroupId("");
        deviceManage.setGroupName("");
        deviceManage.setGroupTime(null);
        deviceManage.setUpdateTime(DateTime.now());
        deviceManage.setUpdateBy(getUsername());
        baseMapper.updateById(deviceManage);
        return AjaxResult.success();
    }

    /**
     * 批量取消设备分组
     */
    @Override
    public AjaxResult groupDeviceCancelAll(String groupId, String[] deviceIds){
        for(String id:deviceIds){
            DeviceManage deviceManage=baseMapper.selectById(id);
            if(ObjectUtil.isEmpty(deviceManage)){
                return AjaxResult.error("未查询到该设备");
            }
            if(deviceManage.getGroupId().equals(groupId)){
                return AjaxResult.error("该设备分配的分组与当前分组不匹配");
            }
            deviceManage.setGroupId("");
            deviceManage.setGroupName("");
            deviceManage.setGroupTime(null);
            deviceManage.setUpdateTime(DateTime.now());
            deviceManage.setUpdateBy(getUsername());
            baseMapper.updateById(deviceManage);
        }
        return AjaxResult.success("取消成功");
    }

    /**
     * 批量选择分组设备
     */
    @Override
    public AjaxResult groupDeviceSelectAll(String groupId, String[] deviceIds){
        DeviceGroup group=deviceGroupMapper.getInfo(groupId);
        if(ObjectUtil.isEmpty(group)){
            return AjaxResult.error("未查询到该设备分组");
        }
        for(String id:deviceIds){
            DeviceManage deviceManage=baseMapper.selectById(id);
            if(ObjectUtil.isEmpty(deviceManage)){
                return AjaxResult.error("未查询到该设备信息");
            }
            deviceManage.setGroupId(groupId);
            deviceManage.setGroupName(group.getGroupName());
            deviceManage.setGroupTime(DateTime.now());
            deviceManage.setUpdateTime(DateTime.now());
            deviceManage.setUpdateBy(getUsername());
            baseMapper.updateById(deviceManage);
        }
        return AjaxResult.success("分配成功");
    }

    /**
     * 首页列表查询
     * @param deviceManage
     * @return
     */
    @Override
    public List<DeviceManage> getDeviceList(DeviceManage deviceManage){
        List<DeviceManage> deviceList=baseMapper.getList(deviceManage);
        for(DeviceManage device:deviceList){
            List<DeviceLineVo> lineVoList=new ArrayList<>();
            if(StringUtils.isNotEmpty(device.getMonitorMainline()) && device.getMonitorMainline().equals("0")){//主线是否开启（0-开启；1-未开启）
                DeviceLineVo lineVo=new DeviceLineVo();
                lineVo.setLineName("0");//(0-主线；1-1号子线；2-2号子线；3-3号子线；4-4号子线)
                lineVo.setStatus(device.getMainStrandState());//主线状态(0-断电；1-通电)
                lineVo.setVoltage("");
                lineVoList.add(lineVo);
            }
            //设备类型(0-单路；1-多路)
            if (device.getDeviceType().equals("1")){
                if(StringUtils.isNotEmpty(device.getMonitorOneStrand()) && device.getMonitorOneStrand().equals("0")){//1号子线是否开启（0-开启；1-未开启）
                    DeviceLineVo lineVo=new DeviceLineVo();
                    lineVo.setLineName("1");
                    lineVo.setStatus(device.getOneStrandState());//1号子线状态(0-断电/缺相；1-通电)
                    lineVo.setVoltage(device.getOneStrandVoltage());
                    lineVoList.add(lineVo);
                }
                if(StringUtils.isNotEmpty(device.getMonitorTwoStrand()) && device.getMonitorTwoStrand().equals("0")){//2号子线是否开启（0-开启；1-未开启）
                    DeviceLineVo lineVo=new DeviceLineVo();
                    lineVo.setLineName("2");
                    lineVo.setStatus(device.getTwoStrandState());//2号子线状态(0-断电/缺相；1-通电)
                    lineVo.setVoltage(device.getTwoStrandVoltage());
                    lineVoList.add(lineVo);
                }
                if(StringUtils.isNotEmpty(device.getMonitorThreeStrand()) && device.getMonitorThreeStrand().equals("0")){//3号子线是否开启（0-开启；1-未开启）
                    DeviceLineVo lineVo=new DeviceLineVo();
                    lineVo.setLineName("3");
                    lineVo.setStatus(device.getThreeStrandState());//3号子线状态(0-断电/缺相；1-通电)
                    lineVo.setVoltage(device.getThreeStrandVoltage());
                    lineVoList.add(lineVo);
                }
                if(StringUtils.isNotEmpty(device.getMonitorFourStrand()) && device.getMonitorFourStrand().equals("0")){//4号子线是否开启（0-开启；1-未开启）
                    DeviceLineVo lineVo=new DeviceLineVo();
                    lineVo.setLineName("4");
                    lineVo.setStatus(device.getFourStrandState());//4号子线状态(0-断电/缺相；1-通电)
                    lineVo.setVoltage(device.getFourStrandVoltage());
                    lineVoList.add(lineVo);
                }
            }
            device.setLineList(lineVoList);
        }
        return deviceList;
    }

    /**
     * 统计设备信息
     * @return
     */
    @Override
    public int staticDeviceNum(){
        return baseMapper.staticDeviceNum();
    }

    /**
     * 统计异常设备信息
     * @return
     */
    @Override
    public int staticUnusualDeviceNum(){
        return baseMapper.staticUnusualDeviceNum();
    }

    /**
     * 统计正常线路信息
     * @return
     */
    @Override
    public int staticLineNum(){
        return baseMapper.staticLineNum();
    }

    /**
     * 统计异常线路信息-1
     * @return
     */
    @Override
    public int staticUnusualLineNum1(){
        return baseMapper.staticUnusualLineNum1();
    }
    /**
     * 统计异常线路信息-2
     * @return
     */
    @Override
    public int staticUnusualLineNum2(){
        return baseMapper.staticUnusualLineNum2();
    }
}
